package jm.mq.direct;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import jm.mq.utils.RabbitMQUtils;

import java.nio.charset.StandardCharsets;

/**
 * @Description 接收 direct路由模式2
 * @date 2022/3/8 9:01
 */
public class Worker2 {
    private static final String EXCHANGE_NAME = "direct_mq";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMQUtils.getChannel();

        // 声明交换机
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);

        // 声明一个队列
        channel.queueDeclare("console",false,false,false,null);
        // 绑定交换机和队列
        channel.queueBind("console",EXCHANGE_NAME,"error");
        System.out.println("线程B等待接收消息...");

        DeliverCallback callback = (consumerTag, message) -> {
            System.out.println("线程B接收到：" + new String(message.getBody(), StandardCharsets.UTF_8));
        };

        channel.basicConsume("console",true,callback,consumerTag -> {});

    }
}
